<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\UserTable;
use App\Admin\Repositories\UserIntegralLog;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Auth;
class UserIntegralLogController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserIntegralLog(['user']), function (Grid $grid) {
            //逆序
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('user.nickname',"用户");
            $grid->column('des')->editable();
            $grid->column('integral');
            $grid->column('recoder');
            $grid->column('checker');
            $grid->column('is_checked')->switch();
            $grid->column('created_at')->sortable();
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal("user_id")->selectTable(UserTable::make())
                    ->title("用户选择")
                    ->model(User::class,'id',"nickname");
                $filter->between("created_at")->datetime();
                $filter->between("updated_at")->datetime();
                $filter->equal("is_checked")
                    ->select([
                        1=>"是",
                        0=>"否"
                    ]);
                $filter->like("recoder");
                $filter->like("checker");


            });
            $grid->disableCreateButton();
            $grid->disableBatchDelete();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserIntegralLog(['user']), function (Show $show) {
            $show->field('id');
            $show->field('user.nickname',"用户名");
            $show->field("user.phone","手机号");
            $show->field('des');
            $show->field('integral');
            $show->field('recoder');
            $show->field('checker');
            $show->field('is_checked');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserIntegralLog(), function (Form $form) {
            $user = User::find(\Request::get("user_id"));
            $form->display('id');
            if ($user){
                $form->hidden("user_id")->default($user->id);
                $form->display('user_name',"用户名")->default($user->nickname);
            }
            $form->number('integral')->required();
            $form->text('des')->required();
            $form->hidden('recoder')->default(Auth::guard('admin')->user()->username);
            $form->hidden('checker')->default(Auth::guard('admin')->user()->username);
            $form->hidden('is_checked')->default(true);
            $form->hidden('is_added')->default(true);

            $form->saving(function (Form $form) use($user) {
                // 判断是否是新增操作
//                if ($form->isCreating()) {
//                    $user->userIntegral()->increment("integral",$form->integral);
//                }
            });
        });
    }
}
